'''
异常捕获
'''

# 创建一个异常 用来捕获
try:
    # 会报出文件不存在异常
    open("D:/project/pythonProject/python/文件操作/文本/异常.txt", "r", encoding="UTF-8")
except:
    print("文件不存在")

# 捕获指定异常
try:
    print(name)
except NameError as e:
    print("变量未定义")
    print(f"异常信息:{e}")

# 捕获多个异常
try:
    1 / 0
except(NameError, ZeroDivisionError) as a:
    print("出现异常")
    print(f"异常信息:{a}")

# 捕获全部异常
try:
    print(name)
except Exception as e:
    print("出现异常")

# 使用 else
try:
    print(2 / 2)
except Exception as e:
    print("出现异常")
else:
    print("没有异常")

# 使用finally（百分百执行）
try:
    print(2 / 2)
except Exception as e:
    print("出现异常")
else:
    print("没有异常")
finally:
    print("执行")